﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using HYView;

namespace YJKStairs.AddSigns
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class TestCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Application application2 = application.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			LevelFinder levelFinder = LevelFinder.GetLevelFinder(document);
			Level genLevel = document.ActiveView.GenLevel;
			levelFinder.GetTopLevel(genLevel.Elevation);
			StairSingleSelectionFilter stairSingleSelectionFilter = new StairSingleSelectionFilter();
			stairSingleSelectionFilter.SetNowLevel(genLevel);
			string appRootPath = Utility.GetAppRootPath();
			string versionNumber = application.Application.VersionNumber;
			StaticRescours.SignFamilyPath = appRootPath + "\\FamilyLib\\Family\\" + versionNumber + "\\StairSign\\";
			StaticRescours.OutPutPath = Path.GetTempPath();
			AddSignManger addSignManger = new AddSignManger(document);
			List<Element> list = activeUIDocument.Selection.PickElementsByRectangle(stairSingleSelectionFilter, "请框选所需扶梯").ToList<Element>();
			int num = 0;
			int num2 = 0;
			foreach (Element element in list)
			{
				if (addSignManger.AddOneSign(element as FamilyInstance, ref num2, null))
				{
					num++;
				}
			}
			addSignManger.DeletTempFiles();
			YJKMessageBox.Show(string.Format("完成，共成功生成 {0}/{1}", num, list.Count));
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
